home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 24 / CU Amiga Magazine's Super CD-ROM 24 (1998)(EMAP Images)(GB)(Track 1 of 2)[!][issue 1998-07].iso / CUCD / Programming / SWI / source / src / plld.sh < prev    next >
Encoding:
Linux/UNIX/POSIX Shell Script  |  1997-08-26  |  3.6 KB  |  160 lines

  1. #!/bin/sh
  2. ################################################################
  3. # plld -- link C and Prolog files to a stand-alone executable
  4. #
  5. # Author: Jan Wielemaker
  6. # E-mail: jan@swi.psy.uva.nl
  7. #
  8. # Copyright 1996 University of Amsterdam, all rights reserved
  9. #
  10. # For documentation, see the SWI-Prolog manual and man plld
  11. ################################################################
  12.  
  13. program="$0"
  14.  
  15. PL=pl
  16. LD=
  17. clibs=
  18. clibdirs=
  19. ldflags=
  20. cfiles=
  21. cflags="-D__SWI_PROLOG__ -D__SWI_EMBEDDED__"
  22. plfiles=
  23. plout="pl.out$$"            # temporary file for saved-state
  24. cout="c.out$$"                # temporary file for emulator
  25. out="a.out"                # the real output file
  26. plgoal='$welcome'            # initial goal (banner)
  27. pltoplevel=prolog            # toplevel goal
  28. plinitfile=none                # file to load at start
  29. makestate=true
  30. verbose=
  31.  
  32. PLLD=true                # for sub-programs
  33. export PLLD
  34.  
  35. usage()
  36. { cat << _EOM_
  37. usage:  $program -help"
  38.     $program [options] c-files ... pl-files ..."
  39.  
  40. options:
  41.     -o out        final output file (default = a.out)
  42.     -goal goal    use 'goal' as the (Prolog) entry-point
  43.     -toplevel goal    use 'goal' as the (Prolog) toplevel (default = prolog)
  44.     -pl prolog    SWI-Prolog executable (default = pl)
  45.     -ld linker    linker to be used (default = <CC used for prolog>)
  46.     -nostate    Do not create a saved state: just link the foreign
  47.             code to the Prolog kernel.
  48.     -initfile file    use 'file' as profile file (default = none)
  49.     -v        verbose: print commands executed
  50.  
  51. Dispatching:
  52.     -g | -I* | -D* | -l* | -L* | *.o | *.c | *.C | *.cpp | *.cxx:
  53.             arguments matching these patterns are passed to
  54.             the C-compiler
  55.     *.pl | *.qlf:
  56.             arguments matching these patterns are passed to
  57.             Prolog
  58. _EOM_
  59. }
  60.  
  61.  
  62. while [ -n "$1" ]; do
  63.   case "$1" in
  64.     -help)
  65.     usage;
  66.     exit 0;;
  67.     -nostate)
  68.     makestate=false;;
  69.     -goal)
  70.     plgoal="$2";
  71.     shift;;
  72.     -toplevel)
  73.     pltoplevel="$2";
  74.     shift;;
  75.     -initfile)
  76.     plinitfile="$2";
  77.     shift;;
  78.     -o)
  79.     out="$2";
  80.     shift;;
  81.     -pl)
  82.     PL="$2";
  83.     shift;;
  84.     -ld)
  85.     LD="$2";
  86.     shift;;
  87.     -v)
  88.         verbose=true;;
  89.     -l*)
  90.     clibs="$clibs $1";;
  91.     -L*)
  92.         clibdirs="$clibdirs $1";;
  93.     -g|-I*|-D*)
  94.     cflags="$cflags $1";;
  95.     *.o|*.c|*.C|*.cxx|*.cpp)
  96.     cfiles="$cfiles $1";;
  97.     *.pl|*.qlf)
  98.         if [ -z "$plfiles" ]; then
  99.       plfiles="'$1'"
  100.     else
  101.       plfiles="$plfiles, '$1'"
  102.     fi;;
  103.     *)    
  104.     ldflags="$ldflags $1";;
  105.   esac
  106.   shift
  107. done
  108.  
  109. if [ "$verbose" = true ]; then echo "%% $PL -dump-runtime-variables"; fi
  110. if eval `$PL -dump-runtime-variables`; then
  111.   true
  112. else
  113.   echo "plld: failed to get runtime variables from $PL"
  114.   exit 1
  115. fi
  116.  
  117. pllibdir="$PLBASE/runtime/$PLARCH"
  118. ldflags="$PLLDFLAGS $ldflags"
  119.  
  120. cleanup ()
  121. { if [ "$verbose" = true ]; then echo "%% rm -f $cout $plout"; fi
  122.   rm -f $cout $plout;
  123. }
  124.  
  125. error()
  126. { cleanup;
  127.   exit 1;
  128. }
  129.  
  130. trap error 1 2 15
  131.  
  132. if [ "$makestate" = false ]; then cout="$out"; fi
  133. if [ -z "$LD" ]; then LD="$CC"; fi
  134.  
  135. LIBS="-L$pllibdir $clibdirs $clibs -lpl $PLLIBS"
  136. ccmd="$LD -o $cout -I$PLBASE/include $cflags $cfiles $ldflags $LIBS"
  137. plflags="-O -f none -F none -g true"
  138.  
  139. if [ "$verbose" = true ]; then echo "%% $ccmd"; fi
  140. if $ccmd; then true; else error; fi
  141.  
  142. if [ "$makestate" = false ]; then
  143.   exit 0;
  144. fi
  145.  
  146. if [ "$verbose" = true ]; then
  147.   echo "%% $PL $plflags -t "'"'"consult([$plfiles]),qsave_program('$plout',[goal='$plgoal',toplevel='$pltoplevel',init_file='$plinitfile'])"'"';
  148. fi
  149. if $PL $plflags -t "consult([$plfiles]),qsave_program('$plout',[goal='$plgoal',toplevel='$pltoplevel',init_file='$plinitfile'])"; then
  150.   true
  151. else
  152.   error
  153. fi
  154. if [ "$verbose" = true ]; then echo "%% cat $cout $plout > $out"; fi
  155. if cat $cout $plout > $out; then true; else error; fi
  156. if [ "$verbose" = true ]; then echo "%% chmod +x $out"; fi
  157. if chmod +x $out; then true; else error; fi
  158. cleanup
  159. exit 0
  160.